package controller

import (
	"encoding/json"
	"github.com/gin-gonic/gin"
	"github.com/jinzhu/gorm"
	"house-property/common"
	"house-property/model"
	"log"
	"net/http"
	"strconv"
)
func House(ctx *gin.Context) {
	DB := common.InitDB()
	b, _ := ctx.GetRawData()
	var m map[string]string
	_ = json.Unmarshal(b, &m)
	unum := m["num"]
	uprofessional := m["professional"]
	upopulation :=m["population"]
	udepartment :=m["department"]
	urequire_hs_ar :=m["area"]
	//unum := ctx.PostForm("unum")
	//uprofessional := ctx.PostForm("uprofessional")
	//upopulation := ctx.PostForm("upopulation")
	//udepartment := ctx.PostForm("udepartment")
	//urequire_hs_ar:=ctx.PostForm("urequirehsar")
	//housing_sco,_:=strconv.Atoi(ctx.PostForm("housingsco"))
	u,_:=strconv.Atoi(upopulation)
	if !isTelephoneExist(DB,unum){
		ctx.JSON(http.StatusOK,gin.H{
			"code":2000,
			"msg":"手机号码输入错误，请重新输入",
		})
		return
	}
	if len(uprofessional) == 0 {
		ctx.JSON(http.StatusOK, gin.H{
			"code": 2001,
			"msg":  "职称不能为空",
		})
		return
	}
	if u == 0 {
		ctx.JSON(http.StatusOK, gin.H{
			"code": 2002,
			"msg":  "家庭人口数不能为空",
		})
		return
	}
	if len(udepartment) == 0 {
		ctx.JSON(http.StatusOK, gin.H{
			"code": 2003,
			"msg":  "部门不能为空",
		})
		return
	}
	if len(urequire_hs_ar) == 0 {
		ctx.JSON(http.StatusOK, gin.H{
			"code": 2004,
			"msg":  "要求住房面积不能为空",
			"hcweh":urequire_hs_ar,
		})
		return
	}
	housing_sco:=calculcation(DB,uprofessional,u,udepartment )

	newUser := model.Separate_ro{
		Unum: unum,
		Udepartment: udepartment,
		Upopulation: u,
		Uprofessional: uprofessional,
		Housing_sco: housing_sco,
		Urequire_hs_ar: urequire_hs_ar,
	}
	//log.Println(newUser.Housing_sco)
	DB.Create(&newUser)
	ctx.JSON(200, gin.H{
		"code":200,
		"msg": "您已成功提交分房申请",
	})

}
func calculcation(db *gorm.DB,uprofessional string,upopulation int,udepartment string)int{
	var de_so model.Department_sco
	var po_so model.Population_sco
	var pr_so model.Professional_sco
	db.Where("DNAME=?",udepartment).First(&de_so)
	db.Where("POPULATION=?",upopulation).First(&po_so)
	db.Where("PNAME=?",uprofessional).First(&pr_so)
	total:=de_so.Dscore+po_so.Pscore+pr_so.Pscore
	return total
}
func Findmsg(ctx *gin.Context){
	DB:=common.InitDB()
	b, _ := ctx.GetRawData()
	var m map[string]string
	_ = json.Unmarshal(b, &m)
	var sta []model.Separate_ro
	DB.Find(&sta)
	ctx.JSON(200,sta)
}
func Separate(ctx *gin.Context){
	DB:=common.InitDB()
	b, _ := ctx.GetRawData()
	var m map[string]string
	_ = json.Unmarshal(b, &m)
	var spa []model.Separate_ro
	DB.Order("housing_sco desc").Find(&spa).Order("housing_sco", true).Find(&spa)
	var hou []model.Hou_property
	DB.Where("hallocation = ?", "否").Find(&hou)
	var h model.Housesta
	a:=len(spa)-1
	i:=0
	j:=0
	for ;i<len(hou)&&a>-1;i++{//从最后开始,当房子分配完或者，没有人之后
		for ;j<len(hou)&&a>-1;j++{//遍历为否的表
			DB.Where("area = ?", spa[a].Urequire_hs_ar).Find(&h)//
			c:=h.Score
			if(spa[a].Urequire_hs_ar==hou[j].Harea && spa[a].Housing_sco>=c){
				DB.Model(&hou).Where("Hnum = ?", hou[j].Hnum).Update("hallocation", "是")//修改符号为是
				b,_:=strconv.Atoi(spa[a].Urequire_hs_ar)
				log.Println("area1",spa[a].Urequire_hs_ar)
				log.Println("area",b)
				log.Println("rent",hou[j].Urentmeter)
				r:= b*hou[j].Urentmeter
				log.Println(r)
				newhouse:=model.House{
					Name:spa[a].Unum,
					Department: spa[a].Udepartment,
					Professinal: spa[a].Uprofessional,
					Population: spa[a].Upopulation,
					Housco: spa[a].Housing_sco,
					Hnum: hou[j].Hnum,
					Harea: spa[a].Urequire_hs_ar,
					Rent: r,
				}
				DB.Create(&newhouse)//插入住房表
				spa2:=spa[a]
				DB.Delete(&spa2)

			}
		}
		log.Println("a",a)
		a=a-1
		//spa[a].Urequire_hs_ar
	}
	ctx.JSON(200,gin.H{
		"code":200,
		"msg":"分配成功",
	})
}
